/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package server;

/**
 *
 * @author User
 */
import engine.Singleton;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import json.ClientProtocol;
import listener.EventProducer;
import listener.MyListener;
import listener.MyRealListener;
import org.json.simple.parser.ParseException;
import tictactoe.SmallField;

public class ConnectClient extends Thread {
  private int port;
  private String login;
  private String addr;
  private Socket socket;
  private BufferedReader in;
  private PrintWriter out;
   private String partnerName;
   public javax.swing.JPanel FieldPannel;
   public SmallField[][] matrix = new SmallField[3][3];
   public static boolean fig;
   public static boolean step;
   public boolean[][] steps = new boolean[3][3];
 
  
  public ConnectClient(int port, String login, String addr) throws IOException{
    this.port = port;
    this.login = login;
    this.addr = addr;
    addr = "localhost";
    //addr = "5.85.70.12";
    port = 8888;
    
    System.out.println("addr = " + addr);
    socket = new Socket(addr, port);
    System.out.println("socket = " + socket);
    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
    
    start();
    
  }
  
  public void disconnect(){
     out.println("{\"Type\": \"End\"}");
  }
  
  public void setPlaer(String plaer){
     out.println("{\"Type\":\"SetPartner\", \"Name\":\""+ plaer +"\", \"OfferName\":\""+ login +"\"}");
  }
  
  
  public void refuse(String send){
     out.println(send);
  }
  
  public void setPartner(String name){
     partnerName = name;
  }
  
  public String getLogin()
  {
    return login;
  }
  
  public String getPartnerName()
  {
    return partnerName;
  }
  
  public void run()
 // public static void connect(int port, String login, String addr) throws IOException, ParseException
  {
    //InetAddress addr = InetAddress.getByName(null);
      
    //ClientProtocol cl = new ClientProtocol(this);
      EventProducer ep = new EventProducer();
      ep.addMyListener(new MyRealListener(this));
    
    try {

      out.println("{\"Type\":\"Auth\", \"Name\":\"" + login + "\"}");
      
      while (true)
            {
                String str = null;
        try {
          str = in.readLine();
        } catch (IOException ex) {
          Logger.getLogger(ConnectClient.class.getName()).log(Level.SEVERE, null, ex);
        }
                if (str.equals("{\"Type\": \"End\"}"))
                {
                  //out.println("{\"Type\": \"End\"}");
                  break;
                }
        try {
          
            ep.getClient(str);
                 // cl.getClient(str);
        } catch (ParseException ex) {
          Logger.getLogger(ConnectClient.class.getName()).log(Level.SEVERE, null, ex);
        }
                System.out.println("Client comming string: " + str);
              
              //    out.println(Singleton.outClient);
                if (Singleton.outClient != null || Singleton.outClient != ""){
                  Singleton.outClient = "";
                }
//                if(str.length() > 1){
//                   str = "";
//                }
                
            }
      
    } finally {
      System.out.println("closing ConnectClient ...");
      try {
        socket.close();
      } catch (IOException ex) {
        Logger.getLogger(ConnectClient.class.getName()).log(Level.SEVERE, null, ex);
      }
    }
    
            try {
                Thread.sleep(1000);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
  }
  
}
